<?php
/**
 * 
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */

class Media   
{
    
   private $Wechat;
   
   public function __construct($Wechat)
    {
        $this->Wechat = $Wechat;
    }
    /**
     * 上传临时素材
     */
     public function uploadMedia()
    {
        $type = isset($_POST['param']) ? $_POST['param'] : 'thumb';
        $media = $this->getMaterial($type);
      
        $data = array('media'=>'@'.$media);
        $result = $this->Wechat->Media->uploadMedia($type,$data);
        return $result;
    }
    /**
     * 获取临时素材
     */
     public function getMedia()
    {
        $media_id = isset($_POST['param']) ? $_POST['param'] :'ZAUvBWVCqafIu2hTkQH0cunXZrJkoiXsX1CHxSQsIuE_w7TshwOPl03VD8fPL-Ly';
        $result = $this->Wechat->Media->getMedia($media_id);
        $file_name = date('YmdHis'). rand(10000, 99999).'.jpg';
        file_put_contents(WECHATSDK_MATERIAL_PATH.$file_name , $result);
        $result = array(
            'file_name' => $file_name,
            'file_path' => WECHATSDK_MATERIAL_PATH.$file_name
        );
        return $result;
    }
    /**
     * 上传永久素材
     */
     public function uploadForeverMedia()
    {
        $type = isset($_POST['param']) ? $_POST['param'] : 'image';
         $media = $this->getMaterial($type);
         $video_info = array();
        if($type =='video')
        {
            $video_info = array('title'=>'视频xx','introduction'=>'视频描述xx');
        }
        $data = array('media'=>'@'.$media);
        $result = $this->Wechat->Media->uploadForeverMedia($type,$data,$video_info);
        return $result;
    }
    private function getMaterial($type='image')
    {
         if($type == 'thumb')
         {
            $file_name = '000.jpg';
         }
         elseif($type == 'image')
         {
             $file_name = '00.jpg';
         }
         elseif($type == 'voice')
         {
             $file_name = '0.mp3';
         }
         elseif($type == 'video')
         {
             $file_name = '0.mp4';
         }
         return WECHATSDK_MATERIAL_PATH.$file_name;
         
    }
    /**
     * 获取永久素材
     */
    public function getForeverMedia()
    {
         $media_id = isset($_POST['param']) ? $_POST['param'] :"tm0D20ZrIxNCja3xm6dVBTCoYwn-OVaprhMAImBIoGA";
        $result = $this->Wechat->Media->getForeverMedia($media_id);
        $file_name = date('YmdHis'). rand(10000, 99999).'.jpg';
        file_put_contents(WECHATSDK_MATERIAL_PATH.$file_name , $result);
        $result = array(
            'file_name' => $file_name,
            'file_path' => WECHATSDK_MATERIAL_PATH.$file_name
        );
       return $result;
    }
    /**
     * 删除永久素材
     */
    public function delForeverMedia()
    {
        $media_id = isset($_POST['param']) ? $_POST['param'] :'tm0D20ZrIxNCja3xm6dVBUCQ_mROTg40LY6sAzo56ck';
        $result = $this->Wechat->Media->delForeverMedia($media_id);
       return $result;
    }
    /**
     * 获取永久素材列表
     */
    public function getForeverList()
    {
         $type = isset($_POST['param']) ? $_POST['param']:"image";
         $offset = 0;
         $count = 20;
        $result = $this->Wechat->Media->getForeverList($type, $offset, $count);
       return $result;
    }
    /**
     * 获取永久素材总数
     */
     public function getForeverCount()
    {
        $result = $this->Wechat->Media->getForeverCount();
       return $result;
    }
    /**
     * 本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制
     */
    public function uploadImg()
    {
         $type = isset($_POST['param']) ? $_POST['param'] : 'image';
         $media = $this->getMaterial($type);
        $data = array('media'=>'@'.$media);
        $result = $this->Wechat->Media->uploadImg($data);
       return $result;
    }
    /**
     * 上传永久图文素材
     */
     public function uploadForeverArticles()
    {
       $data = isset($_POST['param']) && !empty($_POST['param']) ? json_decode($_POST['param'],true) : $this->getArticlesData('forver');
        $result = $this->Wechat->Media->uploadForeverNews($data);
       return $result;
    }
    private function getArticlesData($type='forver')
    {
         $data['forver'] = array('articles'=> array(
                array(
                   'title' =>'永久图文素材' ,
                    'thumb_media_id' =>'tm0D20ZrIxNCja3xm6dVBb2pC6zKhFxLoUgnxkHuzEg' ,
                    
                    'author' =>'作者' ,
                    'digest' =>'图文消息的摘要' ,
                    'show_cover_pic' =>1 ,
                    'content' =>'图文消息的具体内容' ,
                    'content_source_url' =>'http://www.weipai.cn/index.do' ,
                ),
            array(
                   'title' =>'永久图文素材' ,
                    'thumb_media_id' =>'tm0D20ZrIxNCja3xm6dVBccZzqGewDwzHAgEtOZYha8' ,
                    'author' =>'作者' ,
                    'digest' =>'图文消息的摘要' ,
                    'show_cover_pic' =>0 ,
                    'content' =>'图文消息的具体内容' ,
                    'content_source_url' =>'https://www.huxiu.com/' ,
                ),
            
            ));
          $data['article'] = array(
             'articles'=>array(
                array(
                   'title' =>'群发消息素材1' ,
                    'thumb_media_id' =>'Ppxlv08czyzv_ZySUbHdvNSqsB3R18oBPbDqhF7H3XOd0pP30xmSc2GCIQzOfVEZ' ,
                    
                    'author' =>'作者' ,
                    'digest' =>'图文消息的摘要' ,
                    'show_cover_pic' =>1 ,
                    'content' =>'图文消息的具体内容' ,
                    'content_source_url' =>'http://www.weipai.cn/index.do' ,
                ),
            array(
                   'title' =>'群发消息素材2' ,
                    'thumb_media_id' =>'x6ff5xiwAC9O5PSwmbtzFvAghD61mj149ibyA5m_-cc3xs37TLYtXFdvYqjp6Xxz' ,
                    'author' =>'作者' ,
                    'digest' =>'图文消息的摘要' ,
                    'show_cover_pic' =>0 ,
                    'content' =>'图文消息的具体内容' ,
                    'content_source_url' =>'https://www.huxiu.com/' ,
                ),
            )
            );
          $data['update_forver'] = array(
             'articles'=>array(
                   'title' =>'标题xx',
                    'thumb_media_id' =>'tm0D20ZrIxNCja3xm6dVBSNGr9jt1rBhgF_b3sBTZv0' ,
                    'author' =>'作者' ,
                    'digest' =>'图文消息的摘要' ,
                    'show_cover_pic' =>true ,
                    'content' =>'图文消息的具体内容' ,
                    'content_source_url' =>'http://www.weipai.cn/index.do' ,
            ),
             'media_id' => 'tm0D20ZrIxNCja3xm6dVBSx9GF1AtiNWi181fk4crQs',
             'index' => 0,
             );
         return $data[$type];
         
    }
    /**
     * 更新永久图文素材
     */
     public function updateForeverArticles()
    {
         $data = $this->getArticlesData('update_forver');
        $result = $this->Wechat->Media->updateForeverNews($data);
       return $result;
    }
    /**
     * 上传图文消息素材，用于群发
     */
     public function uploadArticles()
    {
        $data = $this->getArticlesData('article');
        $result = $this->Wechat->Media->uploadNews($data);
       return $result;
    }
     /**
     * 上传永久素材
     */
     public function uploadMpVideo()
    {
        $data = array(
            'media_id'=>'gmfNUNgjsQArTiQnsUGsTRMZDvpKPS68_8IIC1mfF69Su_-cFMCiQ00x3TTsk5aD',
            'title'=>'群发素材',
            'description'=>'群发视频需要用到的素材'
            );
        $result = $this->Wechat->Media->uploadMpVideo($data);
       return $result;
    }
    public function __call($name, $arguments)
    {
        exit("Test does not have [{$name}] method !");
    }
     
//index, view, create, update, delete, options
}